package com.zeng.sys.filemd.restfulmodule.server;

import com.zeng.nicecommon.util.ResponseUtil;
import com.zeng.sys.filemd.servicemodule.vo.FileRecordVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * 文件服务器控制器
 */
@RestController
@Slf4j
@RequestMapping
public class ServerController {

    @Value("${sys.file.root:./file_root_data}")
    String root;

    @RequestMapping
    public Object upload(FileRecordVo fileRecordVo, MultipartRequest multipartRequest) {
        MultipartFile file = multipartRequest.getFile(fileRecordVo.getMultipartKey());
        FileOutputStream output = null;
        if (file == null) {
            ResponseUtil.badRequest("没有上传文件");
        }
        try {
            File fileTarget = new File(root, fileRecordVo.getPath());
            if (!fileTarget.getParentFile().exists()) {
                log.debug("文件目录不存在,正在创建");
                if (!fileTarget.getParentFile().mkdirs()) {
                    ResponseUtil.serviceError("创建文件目录错误");
                }
            }
            output = new FileOutputStream(fileTarget);
            IOUtils.copy(file.getInputStream(), output);
            return ResponseUtil.success("成功上传");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.serviceError("服务器错误");
        } finally {
            if (output != null) {
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @RequestMapping
    public void download(String path, HttpServletResponse response) {
        File file = new File(root, path);
        FileInputStream fileInputStream = null;
        if (!file.exists()) {
            response.setStatus(404);
        }
        try {
            fileInputStream = FileUtils.openInputStream(file);
            IOUtils.copy(fileInputStream, response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
            response.setStatus(500);
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    @RequestMapping
    public Object delete(String path) {
        File fileTarget = new File(root, path);
        if (fileTarget.exists()) {
            if (fileTarget.delete()) {
                return ResponseUtil.success("删除成功");
            } else {
                return ResponseUtil.serviceError("删除失败");
            }
        } else {
            return ResponseUtil.badRequest("资源不存在");
        }

    }

    @RequestMapping
    public Object batchDelete(@RequestBody List<String> paths) {
        if (paths.size() <= 0) {
            return ResponseUtil.badRequest("资源不存在");
        }
        for (String path : paths) {
            try {
                File fileTarget = new File(root, path);
                if (fileTarget.exists()) {
                    fileTarget.delete();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return ResponseUtil.success("删除成功");
    }

}
